<?php
class Blog extends MY_Controller
{
	function __construct(){
		parent::__construct();

		$this->load->model(array('/public/user_model', '/public/geo_country_model', '/public/geo_state_model',
								 '/public/geo_city_model', '/public/captcha_model', '/public/contact_us_model',
								 '/public/state_model', '/public/referral_model', '/settings/settings_model',

                                 '/public/bids_model', '/public/inbox_model', '/public/blog_models', '/public/comment_model', '/public/inbox_model'));


		$this->data['Title'] = T_('Blog');

		$this->name = T_('Blog');
		$this->curl = '';
	}
    
	function index(){
        
	  $settings = $this->settings_model->get('blog_posts_page');	  
	  $config = array('per_page' => $settings->value);
      $this->blog_models->blog_all($config);

      $this->renderLayout();	

	}

	function __captcha($string) {
	   $session_id = set_value('captcha_id', 0);
	   if ($this->captcha_model->check($session_id, $string) === false)
	   {
	      return false;
	   }

	   return true;
	}
  
  	function view($br_id){
  	  	$this->blog_models->view($br_id);
  	  	//$this->data['show_or_not'] = $this->auth->getID();
  	 	$this->data['show_or_not'] = $this->settings_model->get('allow_comments');
  	 	
  	  	$this->comment($br_id);
       // $this->load->view('/blog/blog_view',$this->data);
        $this->renderLayout();
        
  	}
  	
  	function comment($br_id) {
	     
  		$postId = $br_id;
  		$postTitle = $this->comment_model->getTitle($br_id);
  	
  		$this->data['br_id'] = $postId;
  		
	     $this->validation->set_fields($this->comment_model->fields);
	     
	     if (count($_POST)) {
	    	$rules = array('id', 'date_created', 'author', 'email', 'text', 'captcha_code');
		 	$this->validation->set_rules($this->comment_model->getRules($rules));
		 	$this->validation->set_message('__captcha', T_('Incorrect verification code.'));
		 	
		 	if ($this->validation->run() === TRUE) {
		 		$item['bc_date_created'] 			= date('Y-m-d H:i:s');
				$item['bc_text'] 					= $this->validation->text;
				$item['bc_status'] 					= 1;				
				$item['bc_author'] 					= $this->validation->author;
				$item['bc_email'] 					= $this->validation->email;
			
				$item['bc_post_id'] 				= $postId;
				$item['bc_under_post'] 				= $postTitle;										
				
				if($this->comment_model->insert($item)) {
					$this->validation->text = '';
					$this->validation->author = '';
					$this->validation->email = '';
					redirect('blog/view/'.$br_id);
				}

				
		 	}
	     }
		 //$this->renderLayout();
  	}

}